define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var Echarts=require("echarts");
    var tool = require('tool');
    var reportDetail = function (callback) {
        this.init.apply(this,arguments);
    };
    reportDetail.prototype = {
        constructor: reportDetail,
        options: {},
        init: function (options) {
            var t = this;
            t.options = $.extend({},t.options,options);
            $(".tipConNav").text("每日日报");
            var id=tool.LockerStr.decrypt(t.options.reportId,'yq');
            t.bindBackClick();
            //加载详情
            t.loadDetail(id);
            t.loadWeixinEvent();
			//滚动条置顶
			window.scrollTo(0,0);
        },
        bindClick:function(id){
        	//导出word
        	$('#expWord').unbind('click').on('click',function(){
        		$("#wordForm").submit();
        	});
        	//导出pdf
        	$('#expPdf').unbind('click').on('click',function(){
        		$("#pdfForm").submit();
        	});

        },
        bindBackClick:function(){
        	//返回
        	$('#backBtn').unbind('click').on('click',function(){
        		window.location.href = '#!admin/report';
        	});
        },
        loadDetail:function(id){
        	var t=this;
       	 	Ajax.ajax({
                url:localhost + "/report/selectOneReport.do?reportId="+id,
                success:function(data){
            		if(data.returnData){
            			var dataDetail={
            					total:data.returnData.reportList[0].total,
            					positiveNum:data.returnData.reportList[0].positiveNum,
            					negativeNum:data.returnData.reportList[0].negativeNum,
            					earlyWarningSize:data.returnData.earlyWarningList.length,
            					HighRiskSize:data.returnData.HighRiskList.length,
            					negativeWarningSize:data.returnData.negativeWarningList.length,
            					reportTime:(data.returnData.reportList[0].reportTime).substring(0,10),
            					reportId:id,
            					expWordurl:localhost+'report/creatDox.do',
            					expPdfurl:localhost+'report/exportPdf.do',
            			};
            			$('#titleDom').html(template('titleTemplate',dataDetail));
            			$('#detailDom').html(template('detailTemplate',dataDetail));
            			//高危预警
            			if(data.returnData.HighRiskList.length>0){
            				var HighRiskList=data.returnData.HighRiskList;
            				for(var i=0;i<HighRiskList.length;i++){
            					HighRiskList[i].author=HighRiskList[i].author==''?'未知':HighRiskList[i].author;
            					if(HighRiskList[i].articleType=="WeiXin"){
            						$('#HighRiskDom').append(template('weixinLiTemplate',HighRiskList[i]));
            					}else{
            						$('#HighRiskDom').append(template('liTemplate',HighRiskList[i]));
            					}
            				}
            			}else{
            				$('#HighRiskDom').parents('.table').remove();
            			}
            			//预警舆情
						if(data.returnData.earlyWarningList.length>0){
							var earlyWarningList=data.returnData.earlyWarningList;
            				for(var i=0;i<earlyWarningList.length;i++){
            					earlyWarningList[i].author=earlyWarningList[i].author==''?'未知':earlyWarningList[i].author;
            					if(earlyWarningList[i].articleType=="WeiXin"){
            						$('#earlyWarningDom').append(template('weixinLiTemplate',earlyWarningList[i]));
            					}else{
            						$('#earlyWarningDom').append(template('liTemplate',earlyWarningList[i]));
            					}
            				}         				
            			}else{
            				$('#earlyWarningDom').parents('.table').remove();
            			}
						//负面舆情
						if(data.returnData.negativeWarningList.length>0){
							var negativeWarningList=data.returnData.negativeWarningList;
            				for(var i=0;i<negativeWarningList.length;i++){
            					negativeWarningList[i].author=negativeWarningList[i].author==''?'未知':negativeWarningList[i].author;
            					if(negativeWarningList[i].articleType=="WeiXin"){
            						$('#negativeWarningDom').append(template('weixinLiTemplate',negativeWarningList[i]));
            					}else{
            						$('#negativeWarningDom').append(template('liTemplate',negativeWarningList[i]));
            					}
            				}
						}else{
							$('#negativeWarningDom').parents('.table').remove();
						}
						//与昨日情况对比
						if(data.returnData.reportList.length>0){
							var reportList=data.returnData.reportList;
            				for(var i=0;i<reportList.length;i++){
            					reportList[i].reportTime=(reportList[i].reportTime).substring(0,10);
            					$('#reportDom').append(template('liSecondTemplate',reportList[i]));
            				}
						}else{
							$('#negativeWarningDom').parents('.table').remove();
						}
						//来源分析
						if(data.returnData.typeNumber){
							var typeNumber=data.returnData.typeNumber;
							var pieChart = echarts.init(document.getElementById('sourceDom'));
				        	var BBS=typeNumber.BBS,Micro=typeNumber.Micro,News=typeNumber.News,WeiXin=typeNumber.WeiXin;
				        	option = {
				        		    title : {
				        		        text: '',
				        		        subtext: '',
				        		        x:'center'
				        		    },
				        		    tooltip : {
				        		        trigger: 'item',
				        		        formatter: "{a} <br/>{b} : {c} ({d}%)"
				        		    },
				        		    legend: {
				        		        orient : 'vertical',
				        		        icon:'line',
				        		        right:'0',
				          		        width:'30',
				          		        bottom:'50',
				        		        data:['论坛','微博','新闻','微信']
				        		    },
				        		    color:[
				    			           '#F44336',
				    			           '#98DD35',
				    			           '#FFC800',
				    			           '#19f21e',
				    		           ],
				        		    toolbox: {
				        		        show : false,
				        		    },
				        		    calculable : true,
				        		    series : [
				        		        {
				        		            name:'',
				        		            type:'pie',
				        		            radius : '55%',
				        		            center: ['50%', '60%'],
				        		            data:[
				        		                {value:BBS, name:'论坛('+BBS+'条)'},
				        		                {value:Micro, name:'微博('+Micro+'条)'},
				        		                {value:News, name:'新闻('+News+'条)'},
				        		                {value:WeiXin, name:'微信('+WeiXin+'条)'},
				        		            ]
				        		        }
				        		    ]
				        		};
				        	pieChart.setOption(option);	   
						}
            		}
            		setTimeout(function(){
                    	html2canvas($('#sourceDom')[0], {  
                    	    onrendered: function (canvas) {  
                    	        var imageUrl = canvas.toDataURL();
                    	        $("#wordsvg").val(imageUrl);
                    	        $("#pdfsvg").val(imageUrl);
                    	        t.bindClick(id);
                    	    }  
                    	}); 
            		},2000);
                }
            });
        },
        
        //微信操作
        loadWeixinEvent:function(){
            var t =this;
            $('.report_table>tbody').delegate('a', 'click', function () {
                var type = $(this).attr('data-type');
                if(type =="weixin"){
                	var articleId = $(this).attr('data-articleId');
                	var ifmId = $(this).attr('data-ifmId');
                	
                	var params ={
                		articleId:articleId,
                		ifmId:ifmId
                	}
                	Ajax.ajax({
        				url: localhost + "Monitoring/findSingle.do",
        				data: params,
        				success: function(data) {
        					if(data.returnCode == 10000){
        	                    var title = data.returnData.title;
        	                    var author = data.returnData.author;
        	                    var releaseTime = data.returnData.releaseTime;
        	                    var source = data.returnData.source;
        	                    var content = data.returnData.content;
        	                    var reg = /\n/g;
        	                    content = content.replace(reg, '<br>');
        	                    layer.open({
        	                        title: '&nbsp;',
        	                        type: 1,
        	                        closeBtn: 1,
        	                        skin: 'inner_url',
        	                        content: "<div class='weixin_title'>" + title + "</div>" +"<div class='weixin_source'><span>"+ releaseTime+"</span><span>"+ author +"</span><span>"+ source+"</span></div><div class='weixin_content'>"+ content + "</div>",
        	                        area: ['80%', '650px'],
        	                        btn:['关闭'],
        	                        maxmin: false,
        	                        scrollbar: false, // 父页面 滚动条 禁止
        	                        shadeClose: true
        	                    });
        					}
        				}
        			});
                }
            });
        }
        
    };
    module.exports = reportDetail;
});